package com.oyjd.beauty.fw.log.action;

import java.io.PrintWriter;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.beauty.fw.action.BaseAction;
import com.oyjd.beauty.fw.constant.FwConst;
import com.oyjd.beauty.fw.log.service.DownLogService;

/**
 * 日志下载Action层
 * 
 * @author 欧建东
 * @version 1.0
 */
@InterceptorRefs({ @InterceptorRef("crudStack") })
@Action(value = "sys/log")
public class DownLogAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8358935529568852782L;
	private DownLogService downLogService;

	@Autowired
	public void setDownLogService(DownLogService downLogService) {
		this.downLogService = downLogService;
	}

	/**
	 * 获取下载列表
	 * 
	 * @return
	 */
	public void getLogList() {
		try {
			String account = ServletRequestUtils.getStringParameter(getRequest(), "account");
			String pwd = ServletRequestUtils.getStringParameter(getRequest(), "pwd");
			if (FwConst.ACCOUNT.equals(account) && FwConst.PWD.equals(pwd)) {
				String root = ServletActionContext.getServletContext().getRealPath("");
				String row = "<td><a href=\"sys/log!downLog.action?id={0}\">{1}</a></td>";
				String logList = downLogService.getLogList(root, row);
				PrintWriter pw = getResponse().getWriter();
				String c = getJspModle(logList);
				pw.write(c);
				pw.close();
			}
		} catch (Exception e) {			
		}
	}

	/**
	 * 获取下载文件
	 */
	public void downLog() {
		try {
			String id = ServletRequestUtils.getStringParameter(getRequest(), "id");
			downLogService.downLog(id, getResponse());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * 清除日志
	 */
	public void deleteLogs() {
		try {
			String account = ServletRequestUtils.getStringParameter(getRequest(), "account");
			String pwd = ServletRequestUtils.getStringParameter(getRequest(), "pwd");
			if (FwConst.ACCOUNT.equals(account) && FwConst.PWD.equals(pwd)) {
				String root = ServletActionContext.getServletContext().getRealPath("");
				downLogService.deleteLogs(root);
				getLogList();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取日志页面模板
	 * 
	 * @return
	 */
	private String getJspModle(String s) {
		StringBuffer sb = new StringBuffer();
		sb.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
		sb.append("<html>\r\n");
		sb.append("	<head>\r\n");
		sb.append("		<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
		sb.append("		<title>下载日志文件</title>\r\n");
		sb.append("	</head>\r\n");
		sb.append("	<body>\r\n");
		sb.append("		<center>\r\n");
		sb.append("			<hr width=\"100%\" />\r\n");
		sb.append("			<table border=\"0\" cellspacing=\"8\" cellpadding=\"1\" width=\"90%\">\r\n");
		sb.append("				").append(s).append("\r\n");
		sb.append("			</table>\r\n");
		sb.append("			<hr width=\"100%\" />\r\n");
		sb.append("			<a href=\"javascript:if(confirm('确定要清除历史日志吗？')){location.href='log!deleteLogs.action?account=" + FwConst.ACCOUNT + "&pwd=" + FwConst.PWD + "';}\">清除历史日志</a>\r\n");
		sb.append("		</center>\r\n");
		sb.append("	</body>\r\n");
		sb.append("</html>\r\n");
		return sb.toString();
	}

}
